Q W E R T Y U I O P A S D F G H J K L Z
X C V B N M
The W at the beginning of the plaintext string maps into V in the
ciphertext, the O maps into G, and so on.
The code for AlphabetSubstitutionCipherin J ava is as follows:
/* File: AlphabetSubstitutionCipher.java* /
public class AlphabetSubstitutionCipher extends
ConsoleP rogram{
public void run(){
println(‘Alphabet-Substitution Cipher”);
String key = readL ine(“E nter 2 6 -alphabet key:”);
String plaintext = readL ine(“P lain text:”);
String ciphertext = encrypt(plaintext, key);
println(“Ciphertext:” + ciphertext);
private String encrypt(String str, String key){
String result = “ “;
str = str.toUpperCase();
for(int i = 0; i< str.length(); i+ + ){
char ch = str.charAt(i);
if(character.isL etter(ch)){
ch = key.charAt(ch – ‘A’);
}
result + = ch;
}
return result;
}
}
The program AlphabetSubstitutionCipher.j ava asks the user for a key
that needs to be checked by the program for its validity. If the key is
not valid, the program provides a message to the user saying that the
key is invalid and will allow the user to give it another try. As an
example, the program is able to duplicate the following sample run:
AlphabetSubstitutionCipher
Enter 26-letter key: SHORTK EY
That key is illegal